package com.bw.module_main.ui.ad

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.lib_common.base.BaseViewModel
import com.bw.lib_storage.AppMMKV
import com.bw.module_main.R
import com.bw.module_main.model.AdModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * 广告页面
 *
 */
@AndroidEntryPoint
@Route(path = Router.Ui.AdActivity)
class AdActivity : BaseActivity<BaseViewModel,ActivityAdBinding>(){
    override val mLayoutResId: Int
        get() = R.layout.activity_ad

    @Autowired
    lateinit var mAdModel:AdModel //接受参数

    private var isOnForward = false //是否处于前台
    private var adCountDownFinished = false //倒计时是否结束

    private var mCountDown = 5
    private val mHandler by lazy{
        object :Handler(Looper.getMainLooper()){
            override fun handleMessage(msg: Message) {
                mCountDown --
                mBinding.tvCountDown.text = String.format("%ds 跳过",mCountDown)
                if (mCountDown == 0){
                    adCountDownFinished = true
                    if (isOnForward){ //保证当前页面处于前台才能跳转到下一页
                        toNextPage()
                    }
                }else{
                    sendEmptyMessageDelayed(1,998)
                }
            }
        }
    }

    override fun initView() {
        super.initView()
        GlideApp.with(this).load(mAdModel.imageUrl).into(mBinding.iv)
    }

    override fun initData() {
        super.initData()
        mHandler.sendEmptyMessageDelayed(1,998)
    }

    override fun initListener() {
        super.initListener()
        //点击跳过
        mBinding.tvCountDown.setOnClickListener {
            mHandler.removeMessages(1)
            toNextPage()
        }
        //点击图片:跳转到广告页面
        mBinding.iv.setOnClickListener {
            if (mAdModel.type == "out"){//外部广告
                showToast("外部广告，跳转web页面加载")
            }else if (mAdModel.type == "in"){ //内部广告
                if (mAdModel.productType == "GoodsDetail"){
                    val goodsId = mAdModel.dataId
                    ARouter.getInstance().build(Router.Ui.GoodsDetailActivity)
                        .withInt("goodsId",goodsId)
                        .navigation()
                }
            }

        }
    }

    fun toNextPage() {
        if (AppMMKV.appGuide){
            toGuide()
        }else{
            toMain()
        }
    }


    fun toGuide(){
        ARouter.getInstance().build(Router.Ui.GuideActivity).navigation()
    }

    fun toMain(){
        ARouter.getInstance().build(Router.Ui.MainActivity).navigation()
    }

    override fun onResume() {
        super.onResume()
        isOnForward = true
        if (adCountDownFinished){
            toNextPage()
        }
    }

    override fun onPause() {
        super.onPause()
        isOnForward = false
    }
    override fun onDestroy() {
        super.onDestroy()
        mHandler.removeCallbacksAndMessages(null) //页面销毁。移除handler中未处理的消息，防止内存泄露
        //页面销毁，activity应该被回收，但是handler持有activity的引用，handler还有未处理完的消息，这样会导致activity无法被回收，最终导致内存泄露
    }
}